import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/pages/Login/Login'
import Home from '@/pages/Home/Home'
import Welcome from '@/pages/Home/Welcome'
import Users from '@/pages/Users/Users'
import Roles from '@/pages/Power/Roles'
import Rights from '@/pages/Power/Rights.vue'
import Categories from '@/pages/Goods/Categories.vue'
import Goods from '@/pages/Goods/Goods.vue'
import Params from '@/pages/Goods/Params.vue'
import Add from '@/pages/Goods/Add.vue'
import Orders from '@/pages/Order/Order.vue'
import Reports from '@/pages/Reports/Reports.vue'

Vue.use(Router)

const router = new Router({
    routes: [
        { path: '/', redirect: "/login" },
        { path: '/login', name: 'login', component: Login },
        {
            path: '/home',
            redirect: '/welcome',
            name: 'home',
            component: Home,
            children: [
                { path: '/welcome', name: 'welcome', component: Welcome },
                { path: '/users', name: 'users', component: Users },
                { path: '/rights', name: 'rights', component: Rights },
                { path: '/roles', name: 'roles', component: Roles },
                { path: '/categories', name: 'categories', component: Categories },
                { path: '/goods', name: 'goods', component: Goods },
                { path: '/params', name: 'params', component: Params },
                { path: '/goods/add', name: 'add', component: Add },
                { path: '/orders', name: 'orders', component: Orders },
                { path: '/reports', name: 'reports', component: Reports }
            ]
        }
    ]
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next();
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next();
})
export default router;
